<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <h2 class="login-title">轻客管家登录</h2>
      <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="User" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码" 
            prefix-icon="Lock" 
            show-password 
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-button" @click="handleLogin" :loading="isLoading">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { saveUserInfo, saveToken } from '../../utils/auth.js'

const router = useRouter()
const loginFormRef = ref(null)
const isLoading = ref(false)

// 登录表单
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
})

// 处理登录
const handleLogin = async () => {
  try {
    await loginFormRef.value.validate()
    isLoading.value = true
    
    // 发送登录请求
    const response = await axios.post('/api/login', {
      username: loginForm.username,
      password: loginForm.password
    })
    
    const result = response.data
    
    if (result.code === 1 && result.data) {
      // 使用工具类保存用户信息和token
      saveUserInfo(result.data)
      saveToken(result.data.token)
      
      ElMessage.success('登录成功')
      router.push('/home')
    } else {
      ElMessage.error(result.msg || '登录失败')
    }
  } catch (error) {
    if (error.response) {
      // 服务器返回错误
      ElMessage.error(error.response.data?.msg || '登录失败')
    } else {
      // 网络错误等
      ElMessage.error('网络错误，请稍后重试')
    }
    console.error('登录失败:', error)
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: url('../../assets/bg.jpg') no-repeat center center;
  background-size: cover;
  position: relative;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
}

.login-form-wrapper {
  width: 400px;
  background: white;
  border-radius: 10px;
  padding: 40px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  position: relative;
  z-index: 1;
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #1e88e5;
  font-size: 24px;
  font-weight: 600;
}

.login-button {
  width: 100%;
  height: 40px;
  font-size: 16px;
}
</style>
